﻿using Sc.Domain;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sc.Persistence
{
    public class CarAuditDAO
    {
        #region  BasicMethod

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(CarAudit model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into CarAudits(");
            strSql.Append("BaseItemId,CarId,AuditDate,AuditYear,OrgzNo,UserId,Remark)");
            strSql.Append(" values (");
            strSql.Append("@BaseItemId,@CarId,@AuditDate,@AuditYear,@OrgzNo,@UserId,@Remark)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@BaseItemId", SqlDbType.Int,4),
					new SqlParameter("@CarId", SqlDbType.Int,4),
					new SqlParameter("@AuditDate", SqlDbType.DateTime),
					new SqlParameter("@AuditYear", SqlDbType.Int,4),
					new SqlParameter("@OrgzNo", SqlDbType.Int,4),
					new SqlParameter("@UserId", SqlDbType.Int,4),
					new SqlParameter("@Remark", SqlDbType.NVarChar,300)};
            parameters[0].Value = model.BaseItemId;
            parameters[1].Value = model.CarId;
            parameters[2].Value = model.AuditDate;
            parameters[3].Value = model.AuditYear;
            parameters[4].Value = model.OrgzNo;
            parameters[5].Value = model.UserId;
            parameters[6].Value = model.Remark;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CarAudit model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update CarAudits set ");
            strSql.Append("BaseItemId=@BaseItemId,");
            strSql.Append("CarId=@CarId,");
            strSql.Append("AuditDate=@AuditDate,");
            strSql.Append("AuditYear=@AuditYear,");
            strSql.Append("OrgzNo=@OrgzNo,");
            strSql.Append("UserId=@UserId,");
            strSql.Append("Remark=@Remark");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@BaseItemId", SqlDbType.Int,4),
					new SqlParameter("@CarId", SqlDbType.Int,4),
					new SqlParameter("@AuditDate", SqlDbType.DateTime),
					new SqlParameter("@AuditYear", SqlDbType.Int,4),
					new SqlParameter("@OrgzNo", SqlDbType.Int,4),
					new SqlParameter("@UserId", SqlDbType.Int,4),
					new SqlParameter("@Remark", SqlDbType.NVarChar,300),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.BaseItemId;
            parameters[1].Value = model.CarId;
            parameters[2].Value = model.AuditDate;
            parameters[3].Value = model.AuditYear;
            parameters[4].Value = model.OrgzNo;
            parameters[5].Value = model.UserId;
            parameters[6].Value = model.Remark;
            parameters[7].Value = model.Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from CarAudits ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from CarAudits ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CarAudit GetModel(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  * From Vw_CarAudits ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;
            CarAudit model = new CarAudit();
            DataTable  dt = DbHelperSQL.Query(CommandType.Text,strSql.ToString(), parameters);
            if (dt.Rows.Count > 0)
            {
                return DataRowToModel(dt.Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CarAudit DataRowToModel(DataRow row)
        {
            CarAudit model = new CarAudit();
            if (row != null)
            {
                if (row["BaseItemName"] != null)
                {
                    model.BaseItemName = row["BaseItemName"].ToString();
                }
                if (row["Id"] != null && row["Id"].ToString() != "")
                {
                    model.Id = int.Parse(row["Id"].ToString());
                }
                if (row["BaseItemId"] != null && row["BaseItemId"].ToString() != "")
                {
                    model.BaseItemId = int.Parse(row["BaseItemId"].ToString());
                }
                if (row["CarId"] != null && row["CarId"].ToString() != "")
                {
                    model.CarId = int.Parse(row["CarId"].ToString());
                }
                if (row["AuditDate"] != null && row["AuditDate"].ToString() != "")
                {
                    model.AuditDate = DateTime.Parse(row["AuditDate"].ToString());
                }
                if (row["AuditYear"] != null && row["AuditYear"].ToString() != "")
                {
                    model.AuditYear = int.Parse(row["AuditYear"].ToString());
                }
                if (row["OrgzNo"] != null && row["OrgzNo"].ToString() != "")
                {
                    model.OrgzNo = int.Parse(row["OrgzNo"].ToString());
                }
                if (row["UserId"] != null && row["UserId"].ToString() != "")
                {
                    model.UserId = int.Parse(row["UserId"].ToString());
                }
                if (row["Remark"] != null)
                {
                    model.Remark = row["Remark"].ToString();
                }
                if (row["CarNo"] != null)
                {
                    model.CarNo = row["CarNo"].ToString();
                }
                if (row["TakeCount"] != null && row["TakeCount"].ToString() != "")
                {
                    model.TakeCount = int.Parse(row["TakeCount"].ToString());
                }
                if (row["ZcTonnage"] != null && row["ZcTonnage"].ToString() != "")
                {
                    model.ZcTonnage = decimal.Parse(row["ZcTonnage"].ToString());
                }
                if (row["CarTypeName"] != null)
                {
                    model.CarTypeName = row["CarTypeName"].ToString();
                }
                if (row["OrgzName"] != null)
                {
                    model.OrgzName = row["OrgzName"].ToString();
                }
            }
            return model;
        }

        #endregion  BasicMethod
    }
}
